// pages/campus-abbr/campus-abbr.js'
let searchDisabled = false
let ifSearch = false
let userInfo = {}
let lastItemNumber = 0
let app = getApp()
const batchSize = 20
var req = require("../../utils/action.js");
var utils = require("../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    scrollLeft:0,
    activeClass: 0,
    activeSubClass: 0,
    itemList: [],
    keyword: '',
    loading: true,
    pageNo:0,
    pageSize:10,
    complete:false,
    schoolId:'',
    schoolList:[]
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id-1)*60
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({schoolId:app.globalData.userInfo.schoolId})
    this.initData()
    wx.setStorageSync('schoolId',app.globalData.userInfo.school_id)
  },
  initData(){
    let data = {customerId:app.globalData.userInfo.id},_url = req.actions.ym+ "school/querySchoolHoleList",that=this
    console.log(data)
    utils.myResquest(data,_url,function(res){
      console.log(JSON.stringify(res))
      that.setData({schoolList:res.data})
    });
    this._loadItemList(0)
  },
  _loadItemList() {
    this.setData({
      loading: true
    }, ()=>{
      let data = {
        "pageNo":this.data.pageNo,
        "pageSize":this.data.pageSize,
        "schoolId":this.data.schoolId,
        "type":5
      },
      that = this
      if(data.schoolId == -1){
        let schoolId = ""
        if(this.data.schoolList.length > 0){
          for(let i=0;i<this.data.schoolList.length;i++){
              schoolId += this.data.schoolList[i].id+","
          }
          schoolId += app.globalData.userInfo.school_id
          data.schoolId = schoolId
        }
      }
      
      console.log(data)
      if(ifSearch) {
        data.keyword = this.data.keyword
      } 
      console.log("data:"+JSON.stringify(data))
      let _url = req.actions.ym+ "invitation/pageList"
      utils.myResquest(data,_url,function(res){
        console.log("_loadItemList===>"+JSON.stringify(res))
        var rows = res.data.result.records
        that.setData({complete:rows && rows.length < 20})
        that.setData({
          itemList: that.data.itemList.concat(rows),
          loading:false
        })
      })
      wx.stopPullDownRefresh()
    })
  },
  onClickSearch() {
    let keyword = this.data.keyword
    if(keyword.trim() == '') {
      wx.showToast({
        title: '搜索关键字不能为空',
        icon: 'none'
      })
      return
    } 
    ifSearch = true
    this._loadItemList(0)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    this.getTabBar().init()
    searchDisabled = false
    userInfo = await getApp().globalData.userInfo
    console.log(userInfo)
    //this.initData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    searchDisabled = false
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({pageNo:0,itemList:[]})
    this._loadItemList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.complete) {
      wx.showToast({
        title: '已全部加载~',
        icon: 'none'
      })
      return
    }
    //this._loadItemList(this.data.itemList.length)
    this.setData({pageNo:this.data.pageNo+1})
    this._loadItemList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '这个很有趣，一起看看吧',
      path: `/pages/campus/campus`,
    }
  },
  onChange(e) {
    console.log(e)
    var index=e.currentTarget.dataset.id;

    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id-1)*60
    })

    ifSearch = false
    if(this.data.activeClass == index)
      return
    this.setData({
      activeClass:index,
      activeSubClass: 0
    })
      if(index == 0){
        this.setData({schoolId : app.globalData.userInfo.schooId})
        wx.setStorageSync('schoolId', app.globalData.userInfo.schoolId)
      }else if(index == 1){
        this.setData({schoolId : -1})
        wx.setStorageSync('schoolId', -1)
      }else{
        this.setData({schoolId : (this.data.schoolList)[index-2].id})
        wx.setStorageSync('schoolId', (this.data.schoolList)[index-2].id)
      }
      console.log("this.data.schoolId:"+this.data.schoolId)
      this._loadItemList()

  },
  onChangeSub(event) {
    ifSearch = false
    if(this.data.activeSubClass == event.detail)
      return
    this.setData({
      activeSubClass:event.detail
    }, ()=>{
      this._loadItemList()
    })
  },
  async onClickPublish() {
    let name = app.globalData.userInfo.name,userInfo = app.globalData.userInfo
    if (!searchDisabled) {
      searchDisabled = true
      if (!name) {
        wx.showToast({
          title: "请先去[我的->个人信息]界面完成身份验证~",
          icon: 'none'
        })
        searchDisabled = false
      } else if (!(userInfo.if_valid == 'Y')) {
        wx.showToast({
          title: "请等待身份验证通过~",
          icon: 'none'
        })
        searchDisabled = false
      } else {
        wx.navigateTo({
          url: `../campus-edit/campus-edit?type=5&schoolId=`+this.data.schoolId,
          success: function(res) {
            // 通过eventChannel向被打开页面传送数据
            res.eventChannel.emit('acceptDataFromOpenerPage', { data: userInfo })
          }
        })
        searchDisabled = false
      }
    }
  },
  goComment(event) {
    wx.navigateTo({
      url: '../../pages/campus-comment/campus-comment?type=0&itemId='+event.currentTarget.dataset.itemId,
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        console.log(event.currentTarget.dataset.item)
        res.eventChannel.emit('acceptDataFromOpenerPage', { 
          data: event.currentTarget.dataset.item,
          userInfo: userInfo,
        })
      }
    })
  },
})